﻿// Required event class
$dh.Require("util/event");
$dh.newClass("DHRichCanvas", DHEvent, {    
    init: function(container, props) {
        this.canvas = $dh.$new("div");
        this.canvas.ownerObj = this;
        $dh.preventLeak(this.canvas, "ownerObj");
        container.appendChild(this.canvas);

        this.setDefaultProps();
        this.setDefaultEvents();
        this.setProps(props);
    },

    setProps: function(props) {
        for (var p in props)
            this.setProp(p, props[p]);
    },
        
    // Overridable method
    setProp: function(p, value) {
        this[p] = value; // By default, all given properties will be instance's properties
        if (("|id|className|title|innerHTML").indexOf("|" + p) >= 0)
            this.canvas[p] = value;
        else if (p == "opacity")
            $dh.opac(this.canvas, value);
        else if (p == "style") // Accept even cssText definition
            $dh.css(this.canvas, value); //this.canvas.style.cssText += ";" + value + ";";
        else if (("|left|top|width|height|right|bottom|fontSize|padding|margin|lineHeight").indexOf("|" + p) >= 0 && $dh.isNum(value)) {
            try {
                this.canvas.style[p] = Math.floor(value) + "px";
            } catch (errrr) {
                this.canvas.style[p] = "0px";
            }
        }
        else if (p == "backgroundImage" && value.toUpperCase().indexOf("URL") < 0) {
            this.canvas.style[p] = "url(" + value + ")";
        }
        else {// By default, given properties will be style 
            try {
                this.canvas.style[p] = value;
            } catch (err) {} // Prevent error on IE8
        }
    },
    setDefault: function(p, value) { // Do nothing with p, let supper class set it
        this._super.prototype.setProp.apply(this, arguments);
    },
    
    getProp: function(p) {
        if (p == "opacity")
            return this.opacity || $dh.opac(this.canvas);
        // paddingLeft, paddingTop, paddintBottom, marginRight,......?
        else if (("|left|top|width|height|fontSize|padding|margin|lineHeight").indexOf("|" + p) >= 0) {
            return this[p] || parseInt($dh.getCssProp(this.canvas, p), 10);
        }
        else
            return this[p];
    },
    
    show: function(flag) {
        this.canvas.style.visibility = (flag == false) ? "hidden" : "visible";
    },
    
    setDefaultProps: function() {
        if (this.__defaults__) {
            for (var p in this.__defaults__)
                this[p] = this.__defaults__[p];
        }
    },
    
    setDefaultEvents: function() {
        var self = this;
        var evList = { mousedown: "MouseDown", mouseup: "MouseUp", mouseover: "MouseOver", mouseout: "MouseOut", mousemove: "MouseMove", click: "Click",
                focus: "Focus", blur: "Blur", scroll: "Scroll", keydown: "KeyDown", keypress: "KeyPress", keyup: "KeyUp"
        };
        for (var p in evList) {
            eval("self.on" + evList[p] + " = self.on" + evList[p] + " || function(){}"); // Create a default blank function
            eval("self." + p + "= function(ev) {self.on" + evList[p] + "(ev);self.raise('on" + p + "',ev);};");
            eval("$dh.addEv(self.canvas,'" + p + "', self." + p + ")");
        }
    }
});